<?php 
if (!isset($_SESSION)) session_start();

$menu_file = "{$_SERVER["DOCUMENT_ROOT"]}/modules/menu/menu.json";
$pending_file = "{$_SERVER["DOCUMENT_ROOT"]}/modules/menu/pending_menu.json";

if (! function_exists('objectToArray'))  {
	function objectToArray($d) {
		if (is_object($d)) {
			// Gets the properties of the given object
			// with get_object_vars function
			$d = get_object_vars($d);
		}
 
		if (is_array($d)) {
			/*
			* Return array converted to object
			* Using __FUNCTION__ (Magic constant)
			* for recursive call
			*/
			return array_map(__FUNCTION__, $d);
		}
		else {
			// Return array
			return $d;
		}
	}
}
	
$string=<<<EOS
{"home":{"text":"Home",
"url":"http://{$_SERVER['SERVER_NAME']}"},

"courses":{"text":"Courses",
"items":[
{"text":"School leavers","section":"school_leavers","url":"school_leavers.html"},
{"text":"Apprenticeships","section":"apprenticeships","url":"apprenticeships.html"},
{"text":"Degree courses","section":"UCP","url":"http://{$_SERVER['SERVER_NAME']}/UCP/index.html"},
{"text":"Adult learning","section":"adult_learning","url":"adult_learning.html"},
{"text":"Business & employers","section":"business_employer","url":"business_employer.html"},
{"text":"International courses","section":"international","url":"international.html"}]},

"UCP":{"alt":"courses"},

"about":{
"text":"About us",
"items":[
{"text":"News & events","section":"news_events","url":"news_events.html"},
{"text":"Quality and Achievements","section":"accreditation","url":"accreditation.html"},
{"text":"Leadership","section":"college_leadership","url":"college_leadership.html"},
{"text":"Governance","section":"governance","url":"Governance.html"}
]},

"services_support":{"text":"Services & Support",
"items":[{"text":"General facilities",
"section":"general_facilities",
"url":"general_facilities.html"},
{"text":"Student Union",
"section":"student_union",
"url":"student_union.html"},
{"text":"The Wellbeing Centre",
"section":"wellbeing_centre",
"url":"wellbeing_centre.html"},
{"text":"Learning support",
"section":"learning_support",
"url":"student_support.html"}]},
"not_just_a_college":{
"text":"Not just a college",
"items":[
{"text":"Nursery","section":"college_nursery","url":"college_nursery.html"},
{"text":"Revive Hair/Beauty","section":"revive","url":"revive.html"},
{"text":"Parcs Restaurant","section":"parcs","url":"parcs.html"},
{"text":"Travel counsellors","section":"travel_counsellors","url":"travel_counsellors.html"},
{"text":"Hire our facilities","section":"hire_facilities","url":"hire_facilities.html"},
{"text":"Epic Retail","section":"epic_retail","url":"epic_retail.html"},
{"text":"Practically Perfect","section":"practically_perfect","url":"practically_perfect.html"}
]},
"development_centres":{"text":"Development Centres",
"items":[{"text":"Rugby development centre",
"section":"rugby_centre",
"url":"rugby_centre.html"},
{"text":"Football development centre",
"section":"football_centre",
"url":"football_centre.html"}]},
"jobs":{"text":"Jobs",
"items":[{"text":"Current vacancies",
"section":"current_vacancies",
"url":"current_vacancies.html"},
{"text":"Recruitment process",
"section":"recruitment_process",
"url":"recruitment_process.html"},
{"text":"Benefits",
"section":"benefits",
"url":"benefits.html"},
{"text":"Charters",
"section":"charters",
"url":"charters.html"},
{"text":"Contact HR",
"section":"contact_hr",
"url":"contact_hr.html"}]},
"contact":{"text":"Contact us",
"items":[{"text":"Find our centres",
"section":"find_our_centres",
"url":"find_our_centres.html"},
{"text":"Email",
"section":"email",
"url":"email.html"},
{"text":"Submit an online enquiry",
"section":"online_enquiry",
"url":"online_enquiry.html"}]}}
EOS;

if (isset($_REQUEST["save"])) {
        //error_reporting(E_ALL);
        //ini_set('display_errors','On');
    if (is_writable($menu_file)) {
	$menu_1 =json_decode($string);
	$menu = objectToArray($menu_1);
	$fp = fopen($menu_file, 'w');
	fwrite($fp, json_encode($menu));
	fclose($fp);
    } else {
        echo "<pre>";
        echo "$menu_file is not writable";
        print_r(stat($menu_file));
        echo "</pre>";
    }
        //echo $menu_file;
        //print_r ($menu);
}

if ((isset($_SESSION["username"]))&&(file_exists($pending_file))) $menu = objectToArray(json_decode(file_get_contents($pending_file)));
else $menu = objectToArray(json_decode(file_get_contents($menu_file)));

if (isset($_REQUEST["text"])) echo json_encode($menu);


?>